package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.EmployeeSCMadd;
import com.zr.service.EmployeeServiceSCM;
import com.zr.vo.Updateqemployee;

/**
 * @author  shichongming
 * @E-mail: 
 * @date 创建时间：2017年7月4日 下午8:04:37 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */

public class EmployeeActionSCM extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{

	private HttpServletRequest req;
	private HttpServletResponse resp;
	private Map<String, Object> session;
	private int rows;
	private int page;	
	private EmployeeSCMadd e;
	private int eild;
	private int eilds;
	private Updateqemployee u;
	
	//属性注入
	@Resource
	private EmployeeServiceSCM employeeService;
	
	
	/**
	 * 展示员工信息
	 * */
	 	public void employeeShow() throws IOException{	
	 
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");	
		
		int total = employeeService.findEmployeetTotal();		
		JSONArray js=employeeService.findEmployeePage(page, rows);		
		JSONObject j=new JSONObject();		
		j.put("total",total);
		j.put("rows",js);
		PrintWriter pw =resp.getWriter();
		pw.write(j.toString());	
	}
	
	
	/**
	 * 展示入职时选择的角色
	 * @throws IOException 
	 * */
	 	public void roleShow() throws IOException{
	 		req.setCharacterEncoding("utf-8");
			resp.setCharacterEncoding("utf-8");	
			JSONArray js=employeeService.findAllRole();
			PrintWriter pw =resp.getWriter();
			pw.write(js.toString());	 		
	 	}
	
	 	/**
		 * 展示入职时选择的角色
	 	 * @throws IOException 
		 */
		 	public void roleDeliverypoint() throws IOException{		 		
		 		req.setCharacterEncoding("utf-8");
				resp.setCharacterEncoding("utf-8");	
				JSONArray js=employeeService.findAllDeliverypoint();
				PrintWriter pw =resp.getWriter();
				pw.write(js.toString());	
		 		
		 	}
	 	
	 	
	 	
	
	/**
	 * 新增员工
	 * @throws IOException 
	 * */
	public void employeeAdd() throws IOException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");	
		//增加员工基本信息
		int  i=employeeService.addEmployee(e.getEiName(), e.getEipostion(), e.getEiPhone(), e.getUname(), e.getUpsw());		
		//增加的员工的用户对应的角色外键
		int a=employeeService.addEmployeeRole(e.getUname(), e.getUpsw(), e.getRid());		
		//获取新员工的id号
		int b=employeeService.findNewEmployeeId(e.getUname(),e.getUpsw());
		//根据新员工的id号，添加配送点名称的id号
		int c=employeeService.addEmployeeDeliverypoint(e.getDpid(), b);			
		PrintWriter pw =resp.getWriter();
		if(i>0){
			pw.write("1");	
		}else{
			pw.write("0");	
		}		
	}
	
	/**
	 * 离职员工，即更改员工状态即可
	 * @throws UnsupportedEncodingException 
	 * 
	 * */
	public void employeeDelete() throws UnsupportedEncodingException{
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");	
		employeeService.deleteEmployee(eild);	
	}
	
	
	/**
	 * 删除员工
	 * @throws UnsupportedEncodingException 
	 * */	
	public void employeeRemove() throws UnsupportedEncodingException{	
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		employeeService.deleteEmployees(eilds);			
	}
	
	
/**
 * 修改用户
 * @throws UnsupportedEncodingException 
 * */
public void employeeUpdate() throws UnsupportedEncodingException{	
	req.setCharacterEncoding("utf-8");
	resp.setCharacterEncoding("utf-8");
		
	employeeService.updateEmployeeinfo(u.getEild(), u.getEipostion(),u.getEiPhone(), u.getUpsw(), u.getEistate(), u.getUstate());
	employeeService.updateEmployeedpid(u.getDpid(), u.getEild());
	int uid=employeeService.findEmployeeUid(u.getEild());
	employeeService.updateEmployeeRid(uid, u.getRid());
}
	

	






	public Updateqemployee getU() {
	return u;
}


public void setU(Updateqemployee u) {
	this.u = u;
}


	public int getEilds() {
		return eilds;
	}


	public void setEilds(int eilds) {
		this.eilds = eilds;
	}


	public EmployeeSCMadd getE() {
		return e;
	}


	public int getEild() {
		return eild;
	}


	public void setEild(int eild) {
		this.eild = eild;
	}


	public void setE(EmployeeSCMadd e) {
		this.e = e;
	}


	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public int getPage() {
		return page;
	}
	
	public void setPage(int page) {
		this.page = page;
	}
	
	

	@Override
	public String execute() throws Exception {
		
		return super.execute();
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		// TODO Auto-generated method stub
		this.resp=arg0;
	}

	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		// TODO Auto-generated method stub
		this.req=arg0;
	}

	@Override
	public void setSession(Map<String, Object> arg0) {
		// TODO Auto-generated method stub
		this.session=arg0;
	}
	
}
